<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <p id="name"></p>
      <p id="age"></p>
      <p id="sex"></p>

      <p id="count"></p>
      <p id="user"></p>
    </div>
    <script type="module">
      // 库给我提供的effect不是副作用函数, 而是用来注册副作用函数的函数
      // effect接收的参数才是副作用函数
      import { reactive, effect, ref } from './04_响应式.js';
      /* 
      // 定义reactive对象
      const state = reactive({
        name: 'tom',
        age: 12
      })

      // 注册三个effect函数
      effect(function effect1() {
        console.log('name effect')
        document.getElementById('name').innerHTML = state.name
      })
      effect(function effect2() {
        console.log('age effect')
        document.getElementById('age').innerHTML = state.age
      })
      effect(() => {
        console.log('sex effect')
        document.getElementById('sex').innerHTML = state.sex
      })

      setTimeout(() => {
        // state.name = 'Jack'
        // state.sex = '男'
        delete state.name

      }, 2000); */

      // const count = ref(1)
      // effect(() => {
      //   console.log('count effect')
      //  document.getElementById('count').innerHTML = count.value
      // })

      const user = ref({
        name: 'tom',
        age: 12
      })
      effect(() => {
        document.getElementById('user').innerHTML = user.value.name + '---' + user.value.age
      })

      setTimeout(() => {
        // count.value = 2

        // user.value = {
        //   name: 'Jack',
        //   age: 13
        // }

        user.value.name = 'BOB'   // ref对象 + reactive对象
      }, 2000);
    </script>
  </body>
</html>
